package com.example.administrator.szb.view;

import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.PopupWindow;

import com.example.administrator.szb.R;
import com.example.administrator.szb.util.QTLog;
import com.github.library.bubbleview.BubbleTextView;

/**
 * Created by stone on 2018/2/3.
 */

public class TipsPop {
    private static PopupWindow popupWindow;
    private static Activity context;

    public static void show(Activity ctx,View view,String str){
        context  = ctx;
        final View popView = LayoutInflater.from(context).inflate(
                R.layout.tipspop, null);
        BubbleTextView tv_tips=popView.findViewById(R.id.tv_tips);
        tv_tips.setText(str);
        int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);

        int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);

//view为填充后得到的View实例

        popView.measure(w, h);
        popupWindow = new PopupWindow(popView, popView.getMeasuredWidth()
                ,popView.getMeasuredHeight(),true);
        ColorDrawable dw = new ColorDrawable(context.getResources().getColor(R.color.white));
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setFocusable(true);// 点击空白处时，隐藏掉pop窗口

        int[] location = new int[2];
        view.getLocationOnScreen(location);
        QTLog.e(popupWindow.getHeight());
        popupWindow.showAtLocation(view, Gravity.NO_GRAVITY,
                location[0], location[1] - popupWindow.getHeight());
        // Gravity.NO_GRAVITY, location[0], location[1] - getMeasureHeight());
    }

    public static void hide(){
        if (popupWindow!=null){
            popupWindow.dismiss();
        }
    }

}
